<template>
  <!-- 如果内容比较多可以添加top属性 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="40%"
    :before-close="handleClose">
    <el-form ref="form" status-icon :model="form" :rules="rules" label-width="80px">
<#list fields as field>
  <#if field.webEditFlug == true>
    <#if field.webEditControl == "select">
        <#include "control/edit/select.ftl">
    </#if>
    <#if field.webEditControl == "input">
        <#include "control/edit/input.ftl">
    </#if>
    <#if field.webEditControl == "yyyyMMddHHmmss">
        <#include "control/edit/yyyyMMddhhmmss.ftl">
    </#if>
    <#if field.webEditControl == "yyyyMMdd">
        <#include "control/edit/yyyyMMdd.ftl">
    </#if>
    <#if field.webEditControl == "radio">
        <#include "control/edit/radiogroup.ftl">
    </#if>
    <#if field.webEditControl == "checkbox">
        <#include "control/edit/checkbox.ftl">
    </#if>
      <#if field.webEditControl == "switch">
          <#include "control/edit/switch.ftl">
      </#if>
  </#if>
</#list>
    </el-form>
    <div class="bottomDiv">
      <el-button @click="handleClose">关闭</el-button>
      <el-button :disabled="disabled" type="primary" @click="save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import api from '@/api'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  name: '${bClassName}Edit',
  data () {
    return {
      title: '',
      <#include "control/edit/enumsData.ftl">
      form: {
     <#list fields as field>
         <#if field.webEditFlug == true>
             <#if field.javaType != 'String'>
        ${field.javaEntityFieldName}: null,
             </#if>
             <#if field.javaType == 'String'>
        ${field.javaEntityFieldName}: '',
             </#if>
         </#if>
     </#list>
      },
      rules: {
      <#list fields as field>
        <#if field.webEditFlug == true>
        ${field.javaEntityFieldName}: [
          { required: true, message: '请输入${field.columnComment}', trigger: 'blur' }
        ],
        </#if>
      </#list>
      }
    }
  },
  methods: {
    <#include "control/edit/enumsMehod.ftl">
    initEunmData () {
      this.getEnumList('key')
    },
    async getEnumList (name) {
      var par = {
        name: name
      }
      var res = await api.GET_ENUMERATION_LIST(par)
      this.enumList = res.body
    },
    setTitle (title) {
      this.title = title
    },
    initData () {
      if (this.$refs.form !== undefined) {
        this.$refs.form.resetFields()
      }
      <#list fields as field>
          <#if field.webEditFlug == true>
      this.form.${field.javaEntityFieldName} = null
          </#if>
      </#list>
    },
    setData (data) {
      if (this.$refs.form !== undefined) {
        this.$refs.form.resetFields()
      }
      <#list fields as field>
        <#if field.webEditFlug == true>
      this.form.${field.javaEntityFieldName} = data.${field.javaEntityFieldName}<#if field.javaType != 'String'> + ''</#if>
        </#if>
      </#list>
    },
    async updateApi () {
      await api.UPDATE_${allbClassName}(this.form)
      this.$emit('initTableData')
      this.$message.success('操作成功')
      this.handleClose()
    },
    async saveApi () {
      await api.SAVE_${allbClassName}(this.form)
      this.$emit('initTableData')
      this.$message.success('操作成功')
      this.handleClose()
    },
    save () {
      this.$refs.form.validate((valid) => {
        if (valid) {
      <#list fields as field>
          <#if field.javaIsPri == '1'>
          if (this.form.${field.javaEntityFieldName} != null) {
          </#if>
      </#list>
            this.$confirm('此操作将永久修改该数据, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.updateApi()
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消修改'
              })
            })
          } else {
            this.saveApi()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose (done) {
      this.$emit('close')
    }
  }
}
</script>

<style>
  .inputText {
    width: 70%;
  }
  .bottomDiv {
    width: 100%;
    text-align: center;
  }
</style>
